package com.znv.sentinelrepository;

import org.springframework.util.Assert;

import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;

/**
 * @author  pengYongQiang
 * @date 2020/7/8 15:31
 * @version 1.0
 */
public class MetricRepositoryConfig {
    private static final Map<MetricRepositoryType, Class<?>> MAPPINGS;
    static {
        EnumMap<MetricRepositoryType, Class<?>> map = new EnumMap<>(MetricRepositoryType.class);
       /* //加载SPI的方式加载扩展的实现
        ServiceLoader<SPIMetricsRepository> load = ServiceLoader.load(SPIMetricsRepository.class);
        Iterator<SPIMetricsRepository> iterator = load.iterator();
        while (iterator.hasNext()){
            SPIMetricsRepository next = iterator.next();
            MetricRepositoryType type = next.getType();
            Class<? extends SPIMetricsRepository> nextClass = next.getClass();
            map.put(type, nextClass);
            System.out.println("Load Metric SPI impl -> type:"+type+"，className:"+nextClass.getName());
        }*/
        //官方实现
     /*   map.put(MetricRepositoryType.MEMORY, InMemoryMetricsRepositoryConfig.class);
        map.put(MetricRepositoryType.MYSQL, InMySqlMetricsRepositoryConfig.class);
*/
        MAPPINGS= Collections.unmodifiableMap(map);
    }
    public static int getSize(){
        return MAPPINGS.size();
    }
    public static String getConfigurationClass(MetricRepositoryType cacheType) {
        Class<?> configurationClass = MAPPINGS.get(cacheType);
        Assert.state(configurationClass != null, () -> "Unknown cache type " + cacheType);
        return configurationClass.getName();
    }
    public static MetricRepositoryType getType(String configurationClassName) {
        for (Map.Entry<MetricRepositoryType, Class<?>> entry : MAPPINGS.entrySet()) {
            if (entry.getValue().getName().equals(configurationClassName)) {
                return entry.getKey();
            }
        }
        throw new IllegalStateException(
                "Unknown configuration class " + configurationClassName);
    }

}
